package com.example.sync.config;

import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @author gaofeng
 * @date 2022年07月03日 11:36
 */
@Configuration
public class RedisConfig {

    @Resource
    RedisConnectionFactory redisConnectionFactory ;

    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>() ;
        // 配置序列化
//        Jackson2JsonRedisSerializer<Object> jsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
//        ObjectMapper mapper = new ObjectMapper() ;
//        mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY) ;
////        mapper.enable(ObjectMapper.DefaultTyping.NON_FINAL) ;
//        mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL) ;
//        jsonRedisSerializer.setObjectMapper(mapper) ;
        // String
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer() ;
        FastJsonRedisSerializer jsonRedisSerializer = new FastJsonRedisSerializer(Object.class) ;
        redisTemplate.setHashKeySerializer(stringRedisSerializer);
        redisTemplate.setHashValueSerializer(jsonRedisSerializer);
        redisTemplate.setKeySerializer(jsonRedisSerializer);
        redisTemplate.setValueSerializer(jsonRedisSerializer);
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        return redisTemplate ;
    }
}
